package cn.itcast.crm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepositoty;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Controller
@Transactional
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerRepositoty customerRepositoty;

	@Override
	public List<Customer> findNoAssciation() {
		List<Customer> list = customerRepositoty.findByFixedAreaIdIsNull();
		return list;
	}

	@Override
	public List<Customer> findAssciation(String fixedAreaId) {
		List<Customer> list = customerRepositoty.findByFixedAreaId(fixedAreaId);
		return list;
	}

	@Override
	public void assciationCustomerToFixedArea(String fixedAreaId,
			String customerIdStr) {
		// 先清除所有的关联信息,然后再重新绑定,否则页面会不停叠加重复显示查询到的信息
		customerRepositoty.clearFixedAreaId(fixedAreaId);
		// 切割字符串
		//customerIdStr过来有可能是"null"和null,所以要做如下判断
		if (StringUtils.isBlank(customerIdStr) || customerIdStr.equals("null")) {
			return;
		}
		String[] arr = customerIdStr.split(",");
		for (String idStr : arr) {
			Integer id = Integer.parseInt(idStr);
			customerRepositoty.updateFixedAreaId(fixedAreaId, id);
		}
	}

}
